package com.chalk.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.chalk.common.constant.CommonConstants;
import com.chalk.common.util.Result;
import com.chalk.config.base.BaseController;
import com.chalk.model.SysParam;
import com.chalk.service.SysParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统参数表(SysParam)表控制层
 *
 * @author maxiaoqiang
 * @since 2019-05-22 15:38:28
 */
@Api(value = "系统参数表接口", tags = "系统参数表模块")
@RestController
@RequestMapping("/v1/sysParam")
public class SysParamController extends BaseController {

    @Autowired
    private SysParamService sysParamService;
    
     /**
     * 系统参数表列表
     *
     * @param current 当前页数
     * @param size 每页显示条数
     * @param sysParam 查询实体
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询系统参数表列表", notes = "查询系统参数表列表", response = Result.class)
    @GetMapping
    public Result selectAll(Integer current, Integer size, SysParam sysParam) {
        return Result.success(this.sysParamService.selectPage(this.getPage(current, size),new EntityWrapper<SysParam>(sysParam)));
    }
    
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询系统参数表", notes = "查询系统参数表", response = Result.class)
    @GetMapping("{id}")
    public Result selectOne(
            @ApiParam(name = "id", value = "主键", required = true, example = "1")
            @PathVariable String id) {
        return Result.success(this.sysParamService.selectById(id));
    }
	
	
	/**
     * 保存系统参数表
     *
     * @param sysParam 实体对象
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "保存sysParam", notes = "保存sysParam", response = Result.class)
    @PostMapping
    public Result insert(
            @ApiParam(name = "sysParam", value = "传入json格式", required = true)
            @RequestBody SysParam sysParam) {
            DateTime date = DateUtil.date();
             sysParam.setCreateTime(date);
             sysParam.setUpdateTime(date);
        return Result.success(this.sysParamService.insert(sysParam));
    }
	
	/**
     * 修改系统参数表
     *
     * @param sysParam 实体对象
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "修改系统参数表", notes = "修改系统参数表", response = Result.class)
    @PutMapping
    public Result update(
            @ApiParam(name = "sysParam", value = "传入json格式", required = true)
            @RequestBody SysParam sysParam) {
             sysParam.setUpdateTime(DateUtil.date());
        return Result.success(this.sysParamService.updateById(sysParam));
    }
	
	/**
     * 逻辑删除系统参数表
     *
     * @param ids 主键集合
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "批量删除系统参数表", notes = "批量删除系统参数表", response = Result.class)
    @DeleteMapping
    public Result delete(
            @ApiParam(name = "ids", value = "传数组", required = true)
            @RequestParam("ids") String [] ids) {
        return Result.success(this.sysParamService.deleteBatchByPrimaryKey(ids));
    }
    
    private Page<SysParam> getPage(Integer current, Integer size){
        Page<SysParam> page = new Page<>();
        if (current != null){ page.setCurrent(current); }
        if (size != null){ page.setSize(size); }
        return page;
    }

    /**
     * 信息管理
     * @param sysParam 查询实体
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询系统参数表列表", notes = "查询系统参数表列表", response = Result.class)
    @GetMapping("/getInfo")
    public Result getInfo(SysParam sysParam) {
        if (null == sysParam.getParamKey() || "".equals(sysParam.getParamKey())) {
            return Result.fail("参数key为空");
        }
        return Result.success(this.sysParamService.selectList( new EntityWrapper<SysParam>(sysParam)
                .eq("is_deleted", CommonConstants.IsDeleted.NOTDEL)
                .eq("service_type",1)
                .eq("param_key",sysParam.getParamKey())));
    }

}